//---------------Sonic CD Breakable Wall Script---------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0	:	Object.XVelocity
#alias Object.Value1	:	Object.YVelocity

// Global SFX
#alias 22	:	SFX_G_EXPLOSION

// Property Values
#alias 0	:	VARIANT_1_SMALL
#alias 1	:	VARIANT_1_BIG_LEFT
#alias 2	:	VARIANT_1_BIG_MIDDLE
#alias 3	:	VARIANT_1_BIG_RIGHT

#alias 4	:	VARIANT_2_SMALL
#alias 5	:	VARIANT_2_BIG_LEFT
#alias 6	:	VARIANT_2_BIG_MIDDLE
#alias 7	:	VARIANT_2_BIG_RIGHT

// from 8 onwards it will be considered debris


sub ObjectMain
	if Object.PropertyValue > 7
		Object.XPos += Object.XVelocity
		Object.YPos += Object.YVelocity
		Object.YVelocity += 0x4000
		if Object.OutOfBounds == true
			Object.Type = TypeName[Blank Object]
		end if
	end if
end sub


sub ObjectPlayerInteraction
	// Checks:
	// That the object is not debris
	// The Player is rolling/jumping, if this one doesn't happen then it will act like a normal wall
	// The Player collided
	if Object.PropertyValue < 8
//		if Player.Animation == ANI_JUMPING // Standalone/Preplus check
		CheckResult = false
		if Player.Animation == ANI_JUMPING
			CheckResult = true
		end if
#platform: Use_Origins
		if Stage.PlayerListPos == PLAYER_KNUCKLES
			CheckResult = true
		end if
		if Stage.PlayerListPos == PLAYER_AMY
			if Player.Animation == ANI_HAMMER_JUMP
				CheckResult = true
			end if
			if Player.Animation == ANI_HAMMER_DASH
				CheckResult = true
			end if
		end if
#endplatform
		if CheckResult == true
#platform: Use_Origins
			PlayerObjectCollision(C_ENEMY, -17, -32, 17, 32)
#endplatform
#platform: Use_Standalone
			PlayerObjectCollision(C_TOUCH, -17, -32, 17, 32)
#endplatform
			if CheckResult == true

				Object.Type = TypeName[Blank Object]
				PlaySfx(SFX_G_EXPLOSION, false)
				TempValue0  = Object.PropertyValue
				TempValue0 *= 8
				TempValue0 += 8

				TempValue1  = Object.XPos
				TempValue1 -= 0x80000

				TempValue2  = Object.XPos
				TempValue2 += 0x80000

				TempValue3  = Object.YPos
				TempValue3 -= 0x100000
				
				TempValue4  = Object.YPos
				TempValue4 += 0x100000

				if Player.XPos < Object.XPos
					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].XVelocity = -0x60000
					Object[TempObjectPos].YVelocity = -0x60000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].XVelocity = -0x40000
					Object[TempObjectPos].YVelocity = -0x50000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					TempValue3 += 0x100000
					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].XVelocity = -0x80000
					Object[TempObjectPos].YVelocity = -0x20000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].XVelocity = -0x60000
					Object[TempObjectPos].YVelocity = -0x10000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					TempValue3 += 0x100000
					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].XVelocity = -0x80000
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].XVelocity = -0x60000
					Object[TempObjectPos].YVelocity = 0x10000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					TempValue3 += 0x100000
					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].XVelocity = -0x60000
					Object[TempObjectPos].YVelocity = 0x60000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].XVelocity = -0x40000
					Object[TempObjectPos].YVelocity = 0x50000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					TempValue3 += 0x100000
				else
					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].XVelocity = 0x40000
					Object[TempObjectPos].YVelocity = -0x50000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].XVelocity = 0x60000
					Object[TempObjectPos].YVelocity = -0x60000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					TempValue3 += 0x100000
					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].XVelocity = 0x60000
					Object[TempObjectPos].YVelocity = -0x10000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].XVelocity = 0x80000
					Object[TempObjectPos].YVelocity = -0x20000
					Object[TempObjectPos].DrawOrder =  4
					TempValue0++

					TempValue3 += 0x100000
					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].XVelocity = 0x60000
					Object[TempObjectPos].YVelocity = 0x10000
					Object[TempObjectPos].DrawOrder = 4
					TempValue0++

					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].XVelocity = 0x80000
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder = 4
					TempValue0++
					
					TempValue3 += 0x100000
					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue1, TempValue3)
					Object[TempObjectPos].XVelocity = 0x40000
					Object[TempObjectPos].YVelocity = 0x50000
					Object[TempObjectPos].DrawOrder = 4
					TempValue0++

					CreateTempObject(TypeName[Breakable Wall], TempValue0, TempValue2, TempValue3)
					Object[TempObjectPos].XVelocity = 0x60000
					Object[TempObjectPos].YVelocity = 0x60000
					Object[TempObjectPos].DrawOrder = 4
					TempValue0++
					TempValue3 += 0x100000
				end if
			end if
		else
			// "Fake" Wall Collision
			PlayerObjectCollision(C_TOUCH, -17, -32, 16, 32)
			if CheckResult == true
				if Player.XPos < Object.XPos
					Player.XPos   = Player.CollisionLeft
					Player.XPos <<= 16
					Player.XPos  += Object.XPos
					Player.XPos  -= 0x100000
					Player.Speed  = 0
					Player.XVelocity = 0
					if Player.Right == true
						if Player.Direction == FACING_RIGHT
							Player.Pushing = 2
						end if
					end if
				else
					Player.XPos   = Player.CollisionRight
					Player.XPos <<= 16
					Player.XPos  += Object.XPos
					Player.XPos  += 0x100000
					Player.Speed  = 0
					Player.XVelocity = 0
					if Player.Direction == FACING_LEFT
						Player.Pushing = 2
					end if
				end if
			end if
		end if
	end if
end sub


sub ObjectDraw
	DrawSprite(Object.PropertyValue)
end sub


sub ObjectStartup
	LoadSpriteSheet("R4.5/Objects.gif")

	// Variant 1
	SpriteFrame(-16, -32, 32, 64, 191, 191)		// #0 - BreakWall singular piece
	SpriteFrame(-16, -32, 32, 64, 207, 191)		// #1 - BreakWall left piece
	SpriteFrame(-16, -32, 32, 64, 223, 191)		// #2 - BreakWall middle piece
	SpriteFrame(-16, -24, 32, 48, 223, 50)		// #3 - BreakWall right piece
	// Variant 2
	SpriteFrame(-16, -32, 32, 64, 191, 191)		// #0 - BreakWall singular piece
	SpriteFrame(-16, -32, 32, 64, 207, 191)		// #1 - BreakWall left piece
	SpriteFrame(-16, -32, 32, 64, 223, 191)		// #2 - BreakWall middle piece
	SpriteFrame(-16, -24, 32, 48, 223, 50)		// #3 - BreakWall right piece
	
	// Variant 1 debris chunks

	// Debris chunks BreakWall singular piece
	TempValue0 = 0
	TempValue3 = 191
	while TempValue0 < 4
		TempValue1 = 0
		TempValue2 = 191
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall left piece
	TempValue0 = 0
	TempValue3 = 191
	while TempValue0 < 4
		TempValue1 = 0
		TempValue2 = 207
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall middle piece
	TempValue0 = 0
	TempValue3 = 191
	while TempValue0 < 4
		TempValue1 = 0
		TempValue2 = 223
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall right piece
	TempValue0 = 0
	TempValue3 = 50
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 223
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Variant 2 debris chunks

	// Debris chunks BreakWall singular piece
	TempValue0 = 0
	TempValue3 = 99
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 190
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall left piece
	TempValue0 = 0
	TempValue3 = 99
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 223
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall middle piece
	TempValue0 = 0
	TempValue3 = 148
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 190
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall right piece
	TempValue0 = 0
	TempValue3 = 148
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 223
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// original code have this below LoadSpriteSheet, moved for consistency with the rest
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Breakable Wall]
			Object[ArrayPos0].DrawOrder = 4
		end if
		ArrayPos0++
	loop
end sub


// ========================
// Editor Subs
// ========================

sub RSDKEdit
	if Editor.ReturnVariable == true
		switch Editor.VariableID
		case EDIT_VAR_PROPVAL // Property Value
			CheckResult = Object.PropertyValue
			CheckResult &= 7
			break
		case 0 // type
			CheckResult = Object.PropertyValue
			CheckResult &= 7
			break
		end switch
	else
		switch Editor.VariableID
		case EDIT_VAR_PROPVAL // Property Value
			Object.PropertyValue = Editor.VariableValue
			Object.PropertyValue &= 7
			break
		case 0 // type
			Object.PropertyValue = Editor.VariableValue
			Object.PropertyValue &= 7
			break
		end switch
	end if
end sub


sub RSDKDraw
	DrawSprite(Object.PropertyValue)
end sub


sub RSDKLoad
	LoadSpriteSheet("R4.5/Objects.gif")

	SpriteFrame(-16, -32, 32, 64, 191, 191)		// #0 - BreakWall singular piece
	SpriteFrame(-16, -32, 32, 64, 207, 191)		// #1 - BreakWall left piece
	SpriteFrame(-16, -32, 32, 64, 223, 191)		// #2 - BreakWall middle piece


	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("left", 0)
	AddEnumVariable("middle", 1)
	AddEnumVariable("right", 2)
end sub
